package paa.crawlers.delicious;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Set;


public class Crawler {

	public static void main(String[] args) throws Exception {
		if (args == null || args.length < 1) {
			System.err.println("usage: java " + Crawler.class + " <out file>");
			System.exit(1);
		}
		
		File file = new File(args[0]);
		if (file.exists()) {
			System.err.println("File exists, exiting.");
			System.exit(1);
		}
		
		//Salvando usuários
		PopularTagBasedUserFinder userFinder = new PopularTagBasedUserFinder();
		Set<String> findUsers = userFinder.findUsers(10);
		
		BufferedWriter writer = new BufferedWriter(new FileWriter(file));
		UserExtractor ue = new UserExtractor();
		for (String id : findUsers) {
			try {
				DeliciousUser user = ue.extractUser(id);
				
				writer.write(user.toString());
				writer.newLine();
				
				Thread.sleep(2000);
				
			} catch (Exception e) {
				System.out.println("Error ocurred: " + e);
			}
		}
		
		writer.flush();
		writer.close();
	}
}
